<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="zh-CN">
<%@include file="include-head.jsp" %>
<link rel="stylesheet" href="css/pagination.css"/>
<script type="text/javascript" src="jquery/jquery.pagination.js"></script>
<script type="text/javascript" src="crowd/my-admin.js"></script>
<script type="text/javascript">
    $(function () {
        // 调用后面声明的函数对页码导航条进行初始化操作
        initPagination();

        // 生成页码导航条的函数
        function initPagination() {

            //获取总记录数
            var totalRecord =${requestScope.pageInfo.total};

            //声名一个JSON对象存储Pagination要设置的属性
            var properties = {
                num_edge_entries: 3, //边缘页数
                num_display_entries: 5, //主体页数
                callback: pageSelectCallback,//指定用户点击翻页的按钮跳转页面的返回
                items_per_page:${requestScope.pageInfo.pageSize}, //每页显示多少项
                current_page:${requestScope.pageInfo.pageNum-1},//页码数量
                prev_text: "上一页",
                next_text: "下一页"
            };
            //生成页码导航条
            $("#Pagination").pagination(totalRecord, properties);
        }

        // 给总的checkbox绑定单击响应函数
        $("#summaryBox").click(function(){

            // ①获取当前多选框自身的状态
            var currentStatus = this.checked;

            // ②用当前多选框的状态设置其他多选框
            $(".itemBox").prop("checked", currentStatus);

        });

        // 全选、全不选的反向操作
        $("#adminPageBody").on("click",".itemBox",function(){

            // 获取当前已经选中的.itemBox的数量
            var checkedBoxCount = $(".itemBox:checked").length;

            // 获取全部.itemBox的数量
            var totalBoxCount = $(".itemBox").length;

            // 使用二者的比较结果设置总的checkbox
            $("#summaryBox").prop("checked", checkedBoxCount == totalBoxCount);

        });

        // 给批量删除的按钮绑定单击响应函数
        $("#deleteBy").click(function () {
            //创建一个数组对象用来存放后面获取到的角色对象
            var adminArray = [];

            //遍历当前选中的多选框
            $(".itemBox:checked").each(function () {

                //使用this引用当前遍历到的多选框
                var adminId = this.id;

                //通过DOM操作获取角色名称
                var adminAcct = $(this).parent().next().text();

                adminArray.push({
                    "adminId":adminId,
                    "adminName": adminAcct
                });
            });

            //检查一下adminArray的长度是否为0
            if (adminArray.length == 0) {
                layer.msg("请至少选择一个执行删除");
                return;
            }
            //调用专门的函数打开模态框
            deleteConfirmModal(adminArray);
        });

        //单击确认模态框中确认要删除的数据
        $("#removeAdminBtn").click(function () {

            // 从全局变量范围获取adminAcctArray，转换为JSON字符串给后端
            var requestBody = JSON.stringify(window.adminAcctArray);

            $.ajax({

                "url": "admin/remove/by/admin/acct/array.json",
                "type": "post",
                "data": requestBody,
                "contentType": "application/json;charset=UTF-8",
                "dataType": "json",
                "success": function (response) {

                    var result = response.result;

                    if (result == "SUCCESS") {
                        layer.msg("操作成功！");


                        //刷新页面
                        window.location.reload();

                    }

                    if (result == "FAILED") {
                        layer.msg("操作失败！" + response.message);
                    }

                },
                "error": function (response) {
                    layer.msg(response.status + " " + response.statusText);
                }

            });
            // 关闭模态框
            $("#adminConfirmModal").modal('hide');

            $("#summaryBox").prop("checked",false);


        });



    });



    //回调函数的含义：声名出来以后不是自己调用，而是交给系统或框架调用
    //用户点击“上一页”“下一页”“1”“2”....这样的页码是调用这个函数实现页面的跳转
    function pageSelectCallback(pageIndex, jQuery) {

        //根据pageIndex计算得到pageNum
        var pageNum = pageIndex + 1;

        //跳转页面
        window.location.href = "admin/get/page.html?pageNum=" + pageNum + "&keyword=${param.keyword}";

        // 由于每一个页码按钮都是超链接，所以在这个函数最后取消超链接的默认行为
        return false;
    }


</script>
<body>

<%@include file="include-nav.jsp" %>
<div class="container-fluid">
    <div class="row">
        <%@include file="include-sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form action="admin/get/page.html" method="post" class="form-inline" role="form"
                          style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input name="keyword" class="form-control has-success" type="text"
                                       placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button type="submit" class="btn btn-warning"><i class="glyphicon glyphicon-search"></i> 查询
                        </button>
                    </form>
                    <button id="deleteBy" type="button" class="btn btn-danger" style="float:right;margin-left:10px;"><i
                            class=" glyphicon glyphicon-remove"></i> 删除
                    </button>
                    <%--<button type="button" class="btn btn-primary" style="float:right;"--%>
                    <%--onclick="window.location.href='add.html'"><i class="glyphicon glyphicon-plus"></i> 新增--%>
                    <%--</button>--%>
                    <a style="float: right;" href="admin/to/add/page.html" class="btn btn-primary"><i
                            class="glyphicon glyphicon-plus">新增</i></a>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr>
                                <th width="30">#</th>
                                <th width="30"><input id="summaryBox" type="checkbox"></th>
                                <th>账号</th>
                                <th>名称</th>
                                <th>邮箱地址</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody id="adminPageBody">
                            <c:if test="${empty requestScope.pageInfo.list}">
                                <tr style="text-align: center">抱歉，没有查询到你要的数据</tr>
                            </c:if>
                            <c:if test="${!empty requestScope.pageInfo.list}">
                            <c:forEach items="${requestScope.pageInfo.list}" var="admin" varStatus="myStatus">
                            <tr>
                                <td>${myStatus.count}</td>
                                <td><input class='itemBox' type="checkbox"></td>
                                <td>${admin.loginAcct}</td>
                                <td>${admin.userName}</td>
                                <td>${admin.email}</td>
                                <td>
                                    <%--<button type="button" class="btn btn-success btn-xs"><i--%>
                                            <%--class=" glyphicon glyphicon-check"></i></button>--%>
                                        <%--<button type="button" class="btn btn-primary btn-xs"><i--%>
                                        <%--class=" glyphicon glyphicon-pencil"></i></button>--%>
                                        <%--<button type="button" class="btn btn-danger btn-xs"><i--%>
                                        <%--class=" glyphicon glyphicon-remove"></i></button>--%>
                                    <a href="assign/role/page.html?adminId=${admin.id }&pageNum=${requestScope.pageInfo.pageNum }&keyword=${param.keyword }"
                                       class="btn btn-success btn-xs">
                                        <i class=" glyphicon glyphicon-check">
                                        </i>
                                    </a>
                                    <a href="admin/to/edit/page.html?adminId=${admin.id }&pageNum=${requestScope.pageInfo.pageNum }&keyword=${param.keyword }"
                                       class="btn btn-primary btn-xs">
                                        <i class=" glyphicon glyphicon-pencil">
                                        </i>
                                    </a>
                                    <a href="admin/remove/${admin.id }/${requestScope.pageInfo.pageNum }/${param.keyword }.html"
                                       class="btn btn-danger btn-xs">
                                        <i class=" glyphicon glyphicon-remove">
                                        </i>
                                    </a>
                                </td>
                                </c:forEach>
                                </c:if>

                            </tr>
                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="6" align="center">
                                    <div id="Pagination" class="pagination"><!-- 这里显示分页 -->
                                    </div>
                                </td>
                            </tr>

                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<%@include file="modal-admin-confirm.jsp"%>
</body>
</html>